<?php

// @formatter:off
/**
 * @file random.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: random.php 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/frontview/actions/content/random.php $
 */
// @formatter:on

class ActionControllerContentRandom extends WCActionController {
	//
	// Public methods.
	public function check() {
		$check = parent::check();

		if($check == WC_HTTPERROR_OK) {
			global $wcProfile;

			if(!$wcProfile->allowedTo(WC_PERM_SPECIAL_RANDOM)) {
				global $errno;
				global $error;

				$check = WC_HTTPERROR_FORBIDDEN;
				$errno = $check;
				$error = "You're not allowed to visit the random items page";
			}
		}

		return $check;
	}
	//
	// Protected methods.
	protected function subRun() {
		$ok = true;

		//
		// Loading global pointers.
		global $wcProfile;
		global $wcReadRate;
		global $wcGroup;
		global $wcGroupForbidden;
		global $wcDefaults;
		global $wcThemeAssigns;
		global $wcUser;

		$this->_manager->PATH = "Randoms";

		$ids = 1;
		$limit = 20;
		$stmtId = basename(__FILE__);
		$db = WCConnection::Instance();
		$dbprefix = $db->prefix();
		//
		// Random Comics
		// @{
		$result = false;
		$stmtC = false;
		if($wcUser) {
			$query = "select  *\n";
			$query .= "from    {$dbprefix}comics\n";
			$query .= "where   cmc_group = :group\n";
			$query .= " and    cmc_lost  = 'N'\n";
			$query .= " and    cmc_id not in (\n";
			$query .= "                select  crr_comic\n";
			$query .= "                from    {$dbprefix}rr_comics\n";
			$query .= "                where   crr_group = :group\n";
			$query .= "                 and    crr_user  = :user\n";
			$query .= "                 and    crr_read  = 'Y'\n";
			$query .= "        )\n";
			$query .= "order by rand()\n";
			$query .= "limit {$limit}";
			$stmtC = $db->prepare("{$stmtId}[1]", $query);

			$params = array(
				":group" => $wcGroup->id,
				":user" => $wcUser->id()
			);
			$stmtC->execute($params);
		}

		if(!$stmtC || ($stmtC !== false && $stmtC->rowCount() < $limit)) {
			$query = "select  *\n";
			$query .= "from    {$dbprefix}comics\n";
			$query .= "where   cmc_group = :group\n";
			$query .= " and    cmc_lost  = 'N'\n";
			$query .= "order by rand()\n";
			$query .= "limit {$limit}";
			$stmtC = $db->prepare("{$stmtId}[2]", $query);

			$params = array(":group" => $wcGroup->id);
			$stmtC->execute($params);
		}

		$error = $stmtC->errorInfo();
		if(!$error[1]) {
			$comics = array();
			foreach($stmtC->fetchAll() as $comicData) {
				$comic = WCComicsHolder::I()->item($comicData["cmc_id"]);
				if($comic && $comic->ok()) {
					$comics[] = WCFVBoxes::GetComicPreviewAssigns($comic);
				}
				unset($comic);
			}

			$this->_manager->comics = $comics;
		}
		// @}
		//
		// Random images.
		// @{
		$result = false;
		$stmtC = false;
		if($wcUser) {
			$query = "select  *\n";
			$query .= "from    {$dbprefix}images\n";
			$query .= "where   img_group = :group\n";
			$query .= " and    img_lost  = 'N'\n";
			$query .= " and    img_id not in (\n";
			$query .= "                select  irr_image\n";
			$query .= "                from    {$dbprefix}rr_images\n";
			$query .= "                where   irr_group = :group\n";
			$query .= "                 and    irr_user  = :user\n";
			$query .= "                 and    irr_read  = 'Y'\n";
			$query .= "        )\n";
			$query .= "order by rand()\n";
			$query .= "limit {$limit}";
			$stmtC = $db->prepare("{$stmtId}[3]", $query);

			$params = array(
				":group" => $wcGroup->id,
				":user" => $wcUser->id()
			);
			$stmtC->execute($params);
		}

		if(!$stmtC || ($stmtC !== false && $stmtC->rowCount() < $limit)) {
			$query = "select  *\n";
			$query .= "from    {$dbprefix}images\n";
			$query .= "where   img_group = :group\n";
			$query .= " and    img_lost  = 'N'\n";
			$query .= "order by rand()\n";
			$query .= "limit {$limit}";
			$stmtC = $db->prepare("{$stmtId}[4]", $query);

			$params = array(":group" => $wcGroup->id);
			$stmtC->execute($params);
		}

		$error = $stmtC->errorInfo();
		if(!$error[1]) {
			$images = array();
			foreach($stmtC->fetchAll() as $imageData) {
				$image = WCImagesHolder::I()->item($imageData["img_id"]);
				if($image && $image->ok()) {
					$images[] = WCFVBoxes::GetImagePreviewAssigns($image);
				}
				unset($image);
			}

			$this->_manager->images = $images;
		}
		// @}
		//
		// Random Tags
		// @{
		$limit = 5;
		$having = 50;

		$query = "select  *\n";
		$query .= "from    {$dbprefix}tags\n";
		$query .= "where   tag_group = :group\n";
		$query .= " and    (tag_count > :having or tag_icount > :having)\n";
		$query .= "order by rand()\n";
		$query .= "limit {$limit}";
		$stmtT = $db->prepare("{$stmtId}[5]", $query);

		$params = array(
			":group" => $wcGroup->id,
			":having" => $having
		);

		$stmtT->execute($params);
		while($stmtT->rowCount() < $limit && $params[":having"] > 0) {
			$params[":having"] -= 10;
			if($params[":having"] < 0) {
				$params[":having"] = 0;
			}
			$stmtT->execute($params);
		}

		if($stmtT->rowCount() > 0) {
			$tags = array();
			foreach($stmtT->fetchAll() as $tagData) {
				$tag = WCTagsHolder::I()->item($tagData["tag_id"]);
				if($tag && $tag->ok()) {
					$tags[$tag->name] = WCFVBoxes::GetTagPreviewAssigns($tag);
				}
			}
			ksort($tags);

			$this->_manager->tags = $tags;
		}
		// @{
		//
		// Random Directories
		// @{
		$limit = 5;
		$having = 50;

		$query = "select  *\n";
		$query .= "from    {$dbprefix}directories\n";
		$query .= "where   dir_group = :group\n";
		$query .= " and    (dir_count > :having or dir_icount > :having)\n";
		$query .= "order by rand()\n";
		$query .= "limit {$limit}";
		$stmtD = $db->prepare("{$stmtId}[6]", $query);

		$params = array(
			":group" => $wcGroup->id,
			":having" => $having
		);

		$stmtD->execute($params);
		while($stmtD->rowCount() < $limit && $params[":having"] > 0) {
			$params[":having"] -= 10;
			if($params[":having"] < 0) {
				$params[":having"] = 0;
			}
			$stmtD->execute($params);
		}

		if($stmtD->rowCount() > 0) {
			$directories = array();
			foreach($stmtD->fetchAll() as $directoryData) {
				$directory = WCDirectoriesHolder::I()->item($directoryData["dir_id"]);
				if($directory && $directory->ok()) {
					$directories[$directory->name] = WCFVBoxes::GetDirectoryPreviewAssigns($directory);
				}
			}
			ksort($directories);

			$this->_manager->directories = $directories;
		}
		// @}

		if(count($this->_manager->comics)) {
			$this->_manager->has_comics = true;
		}
		if(count($this->_manager->images)) {
			$this->_manager->has_images = true;
		}
		if(count($this->_manager->tags)) {
			$this->_manager->has_tags = true;
		}
		if(count($this->_manager->directories)) {
			$this->_manager->has_directories = true;
		}

		return $ok;
	}
}
